package org.jspar.tile;

public class Rectangle {
    
	public int x;
	public int y;
	public int width;
	public int height;
    
	public Rectangle(int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	public int x() {
		return x;
	}
	
	public int y() {
		return y;
	}
	
	public int width() {
		return width;
	}
	
	public int height() {
		return height;
	}
	
	public Position getPosition() {
		return new Position(x, y);
	}
	
	public void setPosition(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	public void setSize(int width, int height) {
		this.width = width;
		this.height = height;
	}
	
	public Rectangle union(Rectangle o) {
		int maxX = x+width;
		int maxY = y+height;
		int minX = x;
		int minY = y;
		if (maxX < 0 || maxX < (o.x() + o.width())) {
			maxX = o.x() + o.width();
		}
		if (maxY < 0 || maxY < (o.y() + o.height())) {
			maxY = o.y() + o.height();
		}
		if (minX > 0 || minX > o.x()) {
			minX = o.x();
		}
		if (minY > 0 || minY > o.y()) {
			minY = o.y();
		}
		return new Rectangle(minX, minY, maxX - minX, maxY - minY);
	}
	
	public Position center() {
		return new Position(x + width/2, y + height/2);
	}

	public String toString() {
		return "R<"+x+" "+y+" "+width+" "+height+">";
	}
	
}
